﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRAdminUI.AttendenceUI
{
    public partial class AttendanceReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void SearchEmployeeAttendance(object o, EventArgs e)
        {
            ViewState["code"] = txtCode.Value;
            GetEmployeeAttendance();
        }

        private void GetEmployeeAttendance()
        {
            AttendanceManager manager = new AttendanceManager();
            List<Attendance> attendances = new List<Attendance>();
            Code code = new Code();
            code.CodeName = ViewState["code"].ToString();
            attendances = manager.GetEmployeeAttendanceInfo(code);
            attendanceReportGdv.DataSource = attendances;
            attendanceReportGdv.DataBind();
            if (attendances.Count > 0)
            {
               
                lblEmpCode.Text = "Employee Code: " + code.CodeName;
            }
            else
            {
                lblEmpCode.ForeColor = Color.Red;
                lblEmpCode.Text = "Employee Code: " + code.CodeName + " has no Attendance Report";
            }
        }

        protected void attendanceReportGdv_RowEditing(object sender, GridViewEditEventArgs e)
        {
            attendanceReportGdv.EditIndex = e.NewEditIndex;
            GetEmployeeAttendance();

        }

        protected void attendanceReportGdv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            attendanceReportGdv.EditIndex = -1;
            GetEmployeeAttendance();
        }

        protected void attendanceReportGdv_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridView gv = (GridView)sender;
            GridViewRow row = gv.Rows[e.RowIndex];
            int id = Convert.ToInt32(((Label)attendanceReportGdv.Rows[e.RowIndex].FindControl("lblId")).Text);
            DropDownList statusddl = (DropDownList)row.FindControl("statusDDL");
            string status  = statusddl.SelectedValue;

            AttendanceManager manager=new AttendanceManager();
            if(manager.UpdateStatus(id,status))
            {
                lblStatusMsg.ForeColor = Color.Green;
                lblStatusMsg.Text = "Status Updated Successfully";
                attendanceReportGdv.EditIndex = -1;
                GetEmployeeAttendance();
            }
        }
    }
}